package com.netease.nieweb.emarket.lottery.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.netease.nieweb.emarket.entity.AwardRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;

import javax.annotation.Resource;
import java.util.UUID;

@EnableBinding(Source.class)
@Slf4j
public class IMessageProviderImpl implements IMessageProvider {

    @Resource
    private MessageChannel output; //消息发送管道

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void send(String id,String actId) {
        JSONObject json=new JSONObject();
        json.put("id",id);
        json.put("activityId",actId);
        try{
            output.send(MessageBuilder.withPayload(json).build());
        }catch (Exception e)
        {
            log.error("send mq message error,change to redis");
            redisTemplate.opsForList().leftPush("LOTTER-JMETER-FAIL",json);
        }
    }
}